﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Common;
using ZFine.Domain.Entity.SystemManage;
using ZFine.Application.SystemManage;

namespace ZFine.Application.Gas
{
    public class ClassApp
    {
        private IClassRepository service = new ClassRepository();

        /// <summary>
        /// 客户端插入
        /// </summary>
        /// <param name="classEntity"></param>
        public void ClientInsert(ClassEntity classEntity)
        {
            classEntity.ClientCreate();
            service.Insert(classEntity);
        }
        public List<ClassEntity> GetList(Pagination pagination)
        {
            var expression = ExtLinq.True<ClassEntity>();
            return service.FindList(expression, pagination);
        }
        /// <summary>
        /// 获得IQueryable
        /// </summary>
        /// <returns></returns>
        public IQueryable<ClassEntity> GetIQueryable(string schoolId, string classValue)
        {
            var expression = ExtLinq.True<ClassEntity>();
            if (!string.IsNullOrEmpty(schoolId))
            {
                expression = expression.And(t => t.SchoolId == schoolId);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            return service.IQueryable(expression);
        }
        public int GetCount(string schoolId, string classValue)
        {
            var expression = ExtLinq.True<ClassEntity>();
            if (!string.IsNullOrEmpty(schoolId))
            {
                expression = expression.And(t => t.SchoolId == schoolId);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            return service.IQueryable(expression).Count();
        }
        public int GetMaxSortId()
        {
            return service.IQueryable().Max(p=>p.SortId).ToInt();
        }
        public ClassEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        //提交
        public void SubmitForm(ClassEntity organizeEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                organizeEntity.Modify(keyValue);
                service.Update(organizeEntity);
            }
            else
            {
                organizeEntity.Create();
                service.Insert(organizeEntity);
            }
        }
        //删除
        public void Del(string keyValue)
        {
            service.Delete(a=>a.F_Id==keyValue);
        }
        //更新
        public void Update(ClassEntity organizeEntity)
        {
            service.Update(organizeEntity);
        }

        //通过学校和年级来查询学位总数
        public int GetClassFreeTotalAmount(string SchoolId, string classValue)
        {
            int returnVlaue = 0;
            var expression = ExtLinq.True<ClassEntity>();
            if (!string.IsNullOrEmpty(SchoolId))
            {
                expression = expression.And(t => t.SchoolId == SchoolId);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            ClassEntity classEntity = service.FindEntity(expression);
            if(null != classEntity)
            {
                returnVlaue = (classEntity.ClassFreeTotalAmount == "") ? 0 : Convert.ToInt32(classEntity.ClassFreeTotalAmount);
            }
            return returnVlaue;
        }

        public ClassEntity GetEntity(string schoolId, string classValue)
        {
            var expression = ExtLinq.True<ClassEntity>();
            if (!string.IsNullOrEmpty(schoolId))
            {
                expression = expression.And(t => t.SchoolId == schoolId);
            }
            if (!string.IsNullOrEmpty(classValue))
            {
                expression = expression.And(t => t.Class == classValue);
            }
            return service.FindEntity(expression);
        }
    }
}
